import type { TextElement } from "@/utils/struct"
import { defineStore } from "pinia"
import { useTextStore } from "./configTextStore"

export function createTextPageStore(storeId:string) {
    return defineStore(storeId, {
        state: () => {
            return {
                pageIndex: 0,
                pageSize: 10,
                elements: [] as TextElement[]
            }
        },
        actions: {
            setPageIndex(idx:number) {
                this.pageIndex = idx
                this.update()
            },
            setPageSize(size:number) {
                this.pageSize = size
                this.update()
            },
            update() {
                let textStore = useTextStore()
                this.elements.splice(0, this.elements.length)
                for (let i = 0; i < this.pageSize; i++) {
                    let idx = i + this.pageIndex * this.pageSize
                    if (idx < textStore.elements.length) {
                        this.elements.push(textStore.elements[idx])
                    }
                }
            }
        }
    })
}

export const useTextPageStore = createTextPageStore("textPageStore")
export const useTextSelectPageStore = createTextPageStore("textSelectPageStore")